﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using ToolUtil;
using zyconfig;
using zymarc;
using zyui;

namespace zymain
{
    public partial class FrmCgxs : Form
    {
        private bool isAutoCheck = false;
        private bool flag = false;
        private int kjm;
        private int gjm;
        private string uid;
        private int fid;
        private string[] fids;
        public BindingSource BookData;
        public BindingSource GcData;
        private DataTable dt = new DataTable();
        private string bsql;
        private string zdd = string.Empty;
        private int ccfs = 0;//查重方式 0isbn,1isbn+题名，2题名+作者
        public FrmCgxs()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }
        public void SetTree()
        {
            this.treeView1.Nodes.Clear();
            using (DbOperHandler _open = new Common().Doh())
            {
                _open.Reset();
                _open.SqlCmd = "select 发行商键码 from 选书征订单 group by 发行商键码 order by 发行商键码";
                DataTable dt = _open.GetDataTable();
                foreach (DataRow dRow in dt.Rows)
                {
                    _open.Reset();
                    _open.ConditionExpress = "键码=" + dRow["发行商键码"];
                    string name = _open.GetField("发行商库", "发行商").ToString().Trim();
                    TreeNode tn1 = new TreeNode();
                    tn1 = treeView1.Nodes.Add(name);
                    _open.Reset();
                    _open.SqlCmd = "select * from 选书征订单 where 发行商键码=" + dRow["发行商键码"];
                    DataTable dr = _open.GetDataTable();
                    foreach (DataRow row in dr.Rows)
                    {
                        TreeNode Ntn1 = new TreeNode(row["征订单号"].ToString().Trim());
                        tn1.Nodes.Add(Ntn1);
                    }
                }
            }
        }
        private void toolStripSplitButton1_DropDownOpening(object sender, EventArgs e)
        {
            ToolStripDropDown toolDrop = new ToolStripDropDown();
            toolDrop.Padding = new Padding(5);
            toolDrop.Font = this.tgcMenuItem.Font;
            toolDrop.Items.Add(this.tgcMenuItem);
            toolDrop.Items.Add(this.dpnMenuItem);
            toolDrop.Show(this, new Point(tcBtn.Bounds.Left + tcBtn.Owner.Left,
                                          tcBtn.Bounds.Bottom + tcBtn.Owner.Top));
        }
        private void FrmCgxs_Load(object sender, EventArgs e)
        {
            string formName = "FrmTool";
            Form fr = System.Windows.Forms.Application.OpenForms[formName];
            if (fr != null)
            {
                FrmTool f1 = (FrmTool)fr;
                //取到Form1的值是1
                this.uid = f1.UserID;
                this.gjm = f1.Gjm;
                this.fid = f1.Fid;
            }
            using (DbOperHandler _open = new Common().Doh())
            {
                _open.Reset();
                _open.ConditionExpress = "代码=" + this.uid;
                fids = _open.GetField("系统用户", "权限").ToString().Split(',');
            }
            List<string[]> list = new List<string[]>();
            this.xsSearch1.AddConfig();
            this.treeView1.ExpandAll();
            this.SetTree();
            this.kjm = this.xsSearch1._kjm;
            this.dataGridXs.Columns[this.dataGridXs.Columns.Count - 1].Visible = false;
            sjq.Panel2Collapsed = !sjq.Panel2Collapsed;
            marcPanel.Panel2Collapsed = !marcPanel.Panel2Collapsed;
        }
        /// <summary>
        /// 获取勾选征订单，若未勾选返回全部
        /// </summary>
        private void GetZdd()
        {
            this.zdd = string.Empty;
            foreach (TreeNode n in treeView1.Nodes)
            {
                if (n.Level == 0 && n.Checked)
                {
                    foreach (TreeNode nr in n.Nodes)
                    {
                        if (nr.Checked)
                            this.zdd += "'" + nr.Text + "',";
                    }
                }
            }
            /**
            if (string.IsNullOrEmpty(this.zdd))
            {
                foreach (TreeNode n in treeView1.Nodes)
                {
                    if (n.Level == 0)
                    {
                        foreach (TreeNode nr in n.Nodes)
                        {
                            this.zdd += "'" + nr.Text + "',";
                        }
                    }
                }
            }
            **/
            if (!string.IsNullOrEmpty(zdd) && zdd.EndsWith(","))
            {
                zdd = zdd.Substring(0, zdd.Length - 1);
            }
        }
        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Node.ForeColor == Color.Gray) { return; }
            if (!isAutoCheck)
            {
                isAutoCheck = true;
                if (e.Node.Checked)
                {
                    TreeViewHandler.checkNode(e.Node);
                }
                else
                {
                    TreeViewHandler.unCheckNode(e.Node);
                }
                isAutoCheck = false;
            }
        }
        private void Search_BtnClick(object sender, EventArgs e)
        {
            this.Invalidate();
            this.kjm = this.xsSearch1._kjm;
            this.Cursor = Cursors.WaitCursor;
            this.bsql = this.xsSearch1.Sql;
            this.BookData = new BindingSource();
            using (DbOperHandler _open = new Common().Doh())
            {
                if (_open.dbType.ToString().Equals("ORACLE") || _open.odbType.ToString().Equals("ORACLE"))
                    flag = true;
                this.GetZdd();
                if (!string.IsNullOrEmpty(zdd))
                {
                    string[] arry = zdd.Split(',');
                    if (arry.Length == 1)
                    {
                        bsql += " and 征订号=" + zdd ;
                    }
                    else
                        bsql += " and 征订号 in (" + zdd + ") ";
                }
                //Console.WriteLine(bsql);
                string sql = "select 书目重复,馆藏重复,是否采购,审核状态,是否送审,订价,订数,主键码,键码,标准编码,题名 ," +
                    "责任者,版次,出版者,出版年,分类,marc " +
                    "from 待采书库 where" + bsql + " order by 标准编码";
                Console.WriteLine(sql);
                _open.Reset();
                _open.SqlCmd = sql;
                dt = _open.GetDataTable();
                dt.Columns.Add("审核");
                foreach(DataRow dr in dt.Rows)
                {
                    int t = Convert.ToInt32(dr["审核状态"]);
                    if (t == 0)
                        dr["审核"] = "未审核";
                    if (t == 1)
                        dr["审核"] = "已通过";
                    if (t == 2)
                        dr["审核"] = "未通过";
                }
                this.dataGridXs.AutoGenerateColumns = false;
                this.BookData.DataSource = dt;
                this.BookData.PositionChanged += new EventHandler(this.BookData_PositionChanged);
                this.dataGridXs.DataSource = this.BookData;
                this.binNavBook.BindingSource = this.BookData;
                if (this.BookData.Count == 0)
                {
                    MessageBox.Show("未检索到任何记录");
                    this.cmBtn.Enabled = false;
                    this.pnBtn.Enabled = false;
                    this.viewBtn.Enabled = false;
                    this.djdMenuItem.Enabled = false;
                    this.tcBtn.Enabled = false;
                    this.ydkBtn.Enabled = false;
                    this.ycgBtn.Enabled = false;
                    this.shBtn.Enabled = false;
                    this.allBtn.Enabled = false;
                    this.qnaBtn.Enabled = false;
                    this.tshBtn.Enabled = false;
                    this.dceMenuItem.Enabled = false;
                }
                else
                {
                    this.cmBtn.Enabled = true;
                    this.pnBtn.Enabled = true;
                    this.viewBtn.Enabled = true;
                    this.BookData_PositionChanged(sender, e);
                    this.djdMenuItem.Enabled = true;
                    this.tcBtn.Enabled = true;
                    this.ydkBtn.Enabled = true;
                    this.ycgBtn.Enabled = true;
                    if(fids.Length>0 && this.fids.Contains(this.fid+"-12"))
                        this.shBtn.Enabled = true;
                    this.allBtn.Enabled = true;
                    this.qnaBtn.Enabled = true;
                    this.tshBtn.Enabled = true;
                    this.dceMenuItem.Enabled = true;
                }
            }
            this.Cursor = Cursors.Default;
        }
        private void BookData_PositionChanged(object sender, EventArgs e)
        {
            DataRowView current = this.BookData.Current as DataRowView;
            string mainKay = current["键码"].ToString().Trim();
            string marc = current["marc"].ToString().Trim();
            string isbn = current["标准编码"].ToString().Trim();
            string tm = current["题名"].ToString().Trim();
            string au = current["责任者"].ToString().Trim();
            string pr = current["出版者"].ToString().Trim();
            bool gcbj = false;
            if(!string.IsNullOrEmpty(current["馆藏重复"].ToString().Trim()))
                gcbj = Convert.ToBoolean(current["馆藏重复"].ToString().Trim());
            //Console.WriteLine("12312312:" + gcbj);
            if (gcbj)
            {
                sjq.Panel2Collapsed = false;
                marcPanel.Panel2Collapsed = false;
            }
            else
            {
                sjq.Panel2Collapsed = true;
                marcPanel.Panel2Collapsed = true;
            }
            if (!string.IsNullOrEmpty(marc))
            {
                this.marcGridXs.key = mainKay;
                //this.marcGridXs.kjm = this.kjm;
                this.marcGridXs.MarcType = 1;
                try
                {
                    MarcConvert convert = new MarcConvert();
                    convert.ISOData = marc;
                    if (marc.Split('\x001e')[0].Length < 25)
                    {
                        convert.MarcText = marc;
                        convert.SetISOStr();
                        convert.SetISOtoMarc();
                    }
                    else
                    {
                        convert.SetISOtoMarc();
                    }
                    this.marcGridXs.MarcText = convert.MarcText;
                    this.marcGridXs.SetGridView();
                }
                catch { }
            }
            using (DbOperHandler _open = new Common().Doh())
            {
                string sql = "RTRIM(LTRIM(g.标准编码))= '" + isbn + "'";
                //Console.WriteLine("123123：" + gcbj);
                if (ccfs == 1)
                    sql = "RTRIM(LTRIM(g.题名))='" + tm + "' ";
                if (ccfs == 2)
                    sql = "RTRIM(LTRIM(g.题名))='" + tm + "' AND RTRIM(LTRIM(g.责任者))= '" + au + "' ";
                if (ccfs == 3)
                    sql = "RTRIM(LTRIM(g.题名.))='" + tm + "' AND RTRIM(LTRIM(g.出版者))= '" + pr + "'";
                sql = "SELECT  d.状态,g.索书号, g.标准编码,g.题名, g.责任者,g.出版者, g.出版日期, g.版次,g.主键码,g.marc From 馆藏书目库 g left join 采购库 d on d.主键码 = g.主键码 WHERE " + sql;
                //Console.WriteLine(sql);
                _open.Reset();
                _open.SqlCmd = sql;
                DataTable dr = _open.GetDataTable();
                var dataRows = dr.AsEnumerable();
                foreach (var row in dataRows)
                {
                    var cellList = row.ItemArray.ToList();
                    row.ItemArray = cellList.Select(x => x.ToString().Trim()).ToArray();
                }
                this.GcData = new BindingSource();
                this.dataGridBook.AutoGenerateColumns = true;
                this.GcData.DataSource = dr;
                this.GcData.PositionChanged += new EventHandler(this.GcData_PositionChanged);
                this.dataGridBook.DataSource = this.GcData;
                if (this.GcData.Count > 0)
                {
                    this.dataGridBook.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);//宽度自适应
                    this.dataGridBook.Columns[this.dataGridBook.Columns.Count - 1].Visible = false;
                    this.dataGridBook.Columns[this.dataGridBook.Columns.Count - 2].Visible = false;
                    this.GcData_PositionChanged(sender, e);
                }
            }
        }
        private void GcData_PositionChanged(object sender, EventArgs e)
        {
            DataRowView current = this.GcData.Current as DataRowView;
            string mainKay = current["主键码"].ToString().Trim();
            string marc = current["marc"].ToString().Trim();
            if (!string.IsNullOrEmpty(marc))
            {
                this.marcGridGc.key = mainKay;
                //this.marcGridXs.kjm = this.kjm;
                this.marcGridGc.MarcType = 1;
                try
                {
                    MarcConvert convert = new MarcConvert();
                    convert.ISOData = marc;
                    if (marc.Split('\x001e')[0].Length < 25)
                    {
                        convert.MarcText = marc;
                        convert.SetISOStr();
                        convert.SetISOtoMarc();
                    }
                    else
                    {
                        convert.SetISOtoMarc();
                    }
                    this.marcGridGc.MarcText = convert.MarcText;
                    this.marcGridGc.SetGridView();
                }
                catch { }
            }
        }
        private void DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            int max = (int)e.Argument;
            using (DbOperHandler _open = new Common().Doh())
            {
                List<int> list = new List<int>();
                for (int i = 0; i < max; i++)
                {
                    worker.ReportProgress((int)((double)i / max * 100), "正在处理第 " + i + " 条数据，请等待...");
                    string zjm = this.dt.Rows[i]["键码"].ToString();
                    string isbn = this.dt.Rows[i]["标准编码"].ToString().Trim();
                    string tm = this.dt.Rows[i]["题名"].ToString().Trim();
                    string au = this.dt.Rows[i]["责任者"].ToString().Trim();
                    string pr = this.dt.Rows[i]["出版者"].ToString().Trim();
                    bool gcbj = false;
                    if(!string.IsNullOrEmpty(this.dt.Rows[i]["馆藏重复"].ToString().Trim()))
                        gcbj = Convert.ToBoolean(this.dt.Rows[i]["馆藏重复"].ToString().Trim());
                    //string sql = "EXISTS(SELECT 主键码 FROM 检索编码库 WHERE 馆藏书目库.主键码 = 检索编码库.主键码 and 数据= '" + isbn + "') AND EXISTS(SELECT 主键码 FROM 馆藏典藏库 WHERE 馆藏书目库.主键码 = 馆藏典藏库.主键码 and 状态 != 'L')";
                    string sql = "RTRIM(LTRIM(标准编码))= '" + isbn + "'";
                    if (ccfs == 1)
                        sql = "RTRIM(LTRIM(题名))='" + tm + "' AND  RTRIM(LTRIM(标准编码))= '" + isbn + "'";
                    if (ccfs == 2)
                        sql = "RTRIM(LTRIM(题名))='" + tm + "' AND RTRIM(LTRIM(责任者))= '" + au + "' ";
                    if (ccfs == 3)
                        sql = "RTRIM(LTRIM(题名))='" + tm + "' AND RTRIM(LTRIM(出版者))= '" + pr + "'";
                    //Console.WriteLine(sql);
                    try
                    {
                        if (!this.qcbjBtn.Checked)
                        {
                            if (!gcbj)
                            {
                                _open.Reset();
                                _open.ConditionExpress = sql;
                                bool flag = _open.Exist("馆藏书目库");
                                if (flag)
                                {
                                    int gzjm = Convert.ToInt32(_open.GetField("馆藏书目库", "主键码").ToString());
                                    _open.Reset();
                                    _open.SqlCmd = "select 状态 from 馆藏典藏库 where 主键码=" + gzjm;
                                    DataTable dr = _open.GetDataTable();
                                    if (dr.Rows.Count>0)
                                    {
                                        foreach(DataRow dRow in dr.Rows)
                                        {
                                            string type = dRow["状态"].ToString().Trim();
                                            if (type.Equals("G"))
                                            {
                                                list.Add(Convert.ToInt32(zjm));
                                                break;
                                            }
                                        }                                        
                                    }
                                    else
                                        list.Add(Convert.ToInt32(zjm));
                                }
                            }
                        }
                        else
                        {
                            _open.Reset();
                            _open.ConditionExpress = sql;
                            bool flag = _open.Exist("馆藏书目库");
                            if (flag)
                            {
                                int gzjm = Convert.ToInt32(_open.GetField("馆藏书目库", "主键码").ToString());
                                _open.Reset();
                                _open.SqlCmd = "select 状态 from 馆藏典藏库 where 主键码=" + gzjm;
                                DataTable dr = _open.GetDataTable();
                                if (dr.Rows.Count > 0)
                                {
                                    foreach (DataRow dRow in dr.Rows)
                                    {
                                        string type = dRow["状态"].ToString().Trim();
                                        if (type.Equals("G"))
                                        {
                                            list.Add(Convert.ToInt32(zjm));
                                            break;
                                        }
                                    }
                                }
                                else
                                    list.Add(Convert.ToInt32(zjm));
                            }
                        }

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("错误提示:" + ex.Message);
                    }
                    Thread.Sleep(5);
                }
                worker.ReportProgress(100, "正在更新数据，请等待...");
                if (list.Count > 0)
                {
                    string allzjm = string.Empty;
                    foreach (var struid in list)
                    {
                        allzjm += "'" + struid + "',";
                    }
                    allzjm = allzjm.Substring(0, allzjm.Length - 1);
                    _open.Reset();
                    _open.ConditionExpress = "键码 in(" + allzjm + ")";
                    _open.AddFieldItem("馆藏重复", 1);
                    _open.Update("待采书库");
                }                
            }
        }
        private void isbnCBtn_Click(object sender, EventArgs e)
        {
            this.ccfs = 0;
            this.Gcc();
            this.Search_BtnClick(sender, e);
        }
        private void Gcc()
        {
            if (this.qcbjBtn.Checked)
            {
                using (DbOperHandler _open = new Common().Doh())
                {
                    _open.Reset();
                    _open.ConditionExpress = this.bsql;
                    _open.AddFieldItem("馆藏重复", 0);
                    _open.Update("待采书库");
                }
            }
            using (ProgressHolder holder = new ProgressHolder(DoWork, "处理中..."))
            {
                holder.DoIt(this.dt.Rows.Count);
            }
        }

        private void isbtBtn_Click(object sender, EventArgs e)
        {
            this.ccfs = 1;
            this.Gcc();
            this.Search_BtnClick(sender, e);
        }

        private void taBtn_Click(object sender, EventArgs e)
        {
            this.ccfs = 2;
            this.Gcc();
            this.Search_BtnClick(sender, e);
        }

        private void tpbtn_Click(object sender, EventArgs e)
        {
            this.ccfs = 2;
            this.Gcc();
            this.Search_BtnClick(sender, e);
        }

        private void isbnPbtn_Click(object sender, EventArgs e)
        {
            FrmWait wait = new FrmWait();
            wait.Show();
            using (DbOperHandler _open = new Common().Doh())
            {
                if (this.qcbjBtn.Checked)
                {
                    _open.Reset();
                    _open.ConditionExpress = this.bsql;
                    _open.AddFieldItem("书目重复", 0);
                    _open.Update("待采书库");
                }
                DataTable dr = DTable.GetDistinctTable(this.dt, "标准编码");
                List<int> list = new List<int>();
                int n = 0;
                foreach (DataRow drow in dr.Rows)
                {
                    wait.label4.Text = (n + 1).ToString();
                    Application.DoEvents();
                    string isbn = drow["标准编码"].ToString().Trim();
                    int m = 0;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string bm = dt.Rows[i]["标准编码"].ToString().Trim();
                        if (bm.Equals(isbn))
                        {
                            m++;
                            if (m > 1)
                                list.Add(i);
                        }
                    }
                }
                string zjms = string.Empty;
                foreach (int j in list)
                {
                    zjms += "'" + dt.Rows[j]["键码"].ToString().Trim() + "',";
                }
                if (!string.IsNullOrEmpty(zjms))
                {
                    zjms = zjms.Substring(0, zjms.Length - 1);
                    _open.Reset();
                    _open.ConditionExpress = "键码 in (" + zjms + ")";
                    _open.AddFieldItem("书目重复", 1);
                    _open.Update("待采书库");
                }
            }
            this.Search_BtnClick(sender, e);
            wait.Close();
        }

        private void qpnBtn_Click(object sender, EventArgs e)
        {
            FrmWait wait = new FrmWait();
            wait.Show();
            using (DbOperHandler _open = new Common().Doh())
            {
                if (this.qcbjBtn.Checked)
                {
                    _open.Reset();
                    _open.ConditionExpress = this.bsql;
                    _open.AddFieldItem("书目重复", 0);
                    _open.Update("待采书库");
                }
                DataTable dr = DTable.GetDistinctTable(this.dt);
                List<int> list = new List<int>();
                int n = 0;
                foreach (DataRow drow in dr.Rows)
                {
                    wait.label4.Text = (n + 1).ToString();
                    Application.DoEvents();
                    string isbn = drow["标准编码"].ToString().Trim();
                    string tm = drow["题名"].ToString().Trim();
                    string au = drow["责任者"].ToString().Trim();
                    string pr = drow["出版者"].ToString().Trim();
                    int m = 0;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string bm = dt.Rows[i]["标准编码"].ToString().Trim();
                        string dtm = dt.Rows[i]["题名"].ToString().Trim();
                        string dau = dt.Rows[i]["责任者"].ToString().Trim();
                        string dpr = dt.Rows[i]["出版者"].ToString().Trim();

                        if (bm.Equals(isbn) && dtm.Equals(tm) && dau.Equals(au) && dpr.Equals(pr))
                        {
                            m++;
                            if (m > 1)
                                list.Add(i);
                        }
                    }
                }
                string zjms = string.Empty;
                foreach (int j in list)
                {
                    zjms += "'" + dt.Rows[j]["键码"].ToString().Trim() + "',";
                }
                zjms = zjms.Substring(0, zjms.Length - 1);
                _open.Reset();
                _open.ConditionExpress = "键码 in (" + zjms + ")";
                _open.AddFieldItem("书目重复", 1);
                _open.Update("待采书库");
            }
            this.Search_BtnClick(sender, e);
            wait.Close();
        }

        private void tzpnBtn_Click(object sender, EventArgs e)
        {
            FrmWait wait = new FrmWait();
            wait.Show();
            using (DbOperHandler _open = new Common().Doh())
            {
                if (this.qcbjBtn.Checked)
                {
                    _open.Reset();
                    _open.ConditionExpress = this.bsql;
                    _open.AddFieldItem("书目重复", 0);
                    _open.Update("待采书库");
                }
                DataTable dr = DTable.GetDistinctTable(this.dt, "题名", "责任者");
                List<int> list = new List<int>();
                int n = 0;
                foreach (DataRow drow in dr.Rows)
                {
                    wait.label4.Text = (n + 1).ToString();
                    Application.DoEvents();
                    string tm = drow["题名"].ToString().Trim();
                    string au = drow["责任者"].ToString().Trim();
                    int m = 0;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string dtm = dt.Rows[i]["题名"].ToString().Trim();
                        string dau = dt.Rows[i]["责任者"].ToString().Trim();
                        if (dtm.Equals(tm) && dau.Equals(au))
                        {
                            m++;
                            if (m > 1)
                                list.Add(i);
                        }
                    }
                }
                string zjms = string.Empty;
                foreach (int j in list)
                {
                    zjms += "'" + dt.Rows[j]["键码"].ToString().Trim() + "',";
                }
                zjms = zjms.Substring(0, zjms.Length - 1);
                _open.Reset();
                _open.ConditionExpress = "键码 in (" + zjms + ")";
                _open.AddFieldItem("书目重复", 1);
                _open.Update("待采书库");
            }
            this.Search_BtnClick(sender, e);
            wait.Close();
        }

        private void itPbtn_Click(object sender, EventArgs e)
        {
            FrmWait wait = new FrmWait();
            wait.Show();
            using (DbOperHandler _open = new Common().Doh())
            {
                if (this.qcbjBtn.Checked)
                {
                    _open.Reset();
                    _open.ConditionExpress = this.bsql;
                    _open.AddFieldItem("书目重复", 0);
                    _open.Update("待采书库");
                }
                DataTable dr = DTable.GetDistinctTable(this.dt, "标准编码", "题名");
                List<int> list = new List<int>();
                int n = 0;
                foreach (DataRow drow in dr.Rows)
                {
                    wait.label4.Text = (n + 1).ToString();
                    Application.DoEvents();
                    string isbn = drow["标准编码"].ToString().Trim();
                    string tm = drow["题名"].ToString().Trim();
                    int m = 0;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string bm = dt.Rows[i]["标准编码"].ToString().Trim();
                        string dtm = dt.Rows[i]["题名"].ToString().Trim();
                        if (bm.Equals(isbn) && dtm.Equals(tm))
                        {
                            m++;
                            if (m > 1)
                                list.Add(i);
                        }
                    }
                }
                string zjms = string.Empty;
                foreach (int j in list)
                {
                    zjms += "'" + dt.Rows[j]["键码"].ToString().Trim() + "',";
                }
                zjms = zjms.Substring(0, zjms.Length - 1);
                _open.Reset();
                _open.ConditionExpress = "键码 in (" + zjms + ")";
                _open.AddFieldItem("书目重复", 1);
                _open.Update("待采书库");
            }
            this.Search_BtnClick(sender, e);
            wait.Close();
        }

        private void viewBtn_Click(object sender, EventArgs e)
        {
            if (sjq.Panel2Collapsed)
            {
                sjq.Panel2Collapsed = false;
                marcPanel.Panel2Collapsed = false;
            }
            else
            {
                sjq.Panel2Collapsed = true;
                marcPanel.Panel2Collapsed = true;
            }
        }

        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void isoMenuItem_Click(object sender, EventArgs e)
        {
            this.kjm = this.xsSearch1._kjm;
            FrmCxIso iso = new FrmCxIso();
            iso.uid = this.uid;
            iso.kjm = this.kjm;
            if (iso.ShowDialog() == DialogResult.OK)
            {
                this.SetTree();
            }
        }

        private void djdMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认要删除检索到的记录吗？", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                FrmWait wait = new FrmWait();
                wait.Show();
                using (DbOperHandler _open = new Common().Doh())
                {
                    string jms = string.Empty;
                    int i = 0;
                    foreach (DataRow drow in this.dt.Rows)
                    {
                        i++;
                        wait.label4.Text = "正在处理 " + i.ToString();
                        Application.DoEvents();
                        jms += "'" + drow["键码"].ToString().Trim() + "',";
                    }
                    if (!string.IsNullOrEmpty(jms))
                    {
                        jms = jms.Substring(0, jms.Length - 1);
                        _open.Reset();
                        _open.ConditionExpress = "键码 in(" + jms + ")";
                        _open.Delete("待采书库");
                        string zdd = string.Empty;
                        foreach (TreeNode n in treeView1.Nodes)
                        {
                            if (n.Level == 0 && n.Checked)
                            {
                                foreach (TreeNode nr in n.Nodes)
                                {
                                    if (nr.Checked)
                                        zdd += "'" + nr.Text + "',";
                                }
                            }
                        }
                        if (!string.IsNullOrEmpty(this.zdd))
                        {
                            _open.Reset();
                            _open.ConditionExpress = "征订单号 in(" + this.zdd + ")";
                            _open.Delete("选书征订单");
                            this.SetTree();
                        }
                        this.dataGridXs.DataSource = null;
                        this.dataGridBook.DataSource = null;
                        this.sjq.Panel2Collapsed = true;
                        this.marcPanel.Panel2Collapsed = true;
                        this.marcGridXs.MarcText = "";
                        this.marcGridXs.SetGridView();
                        this.marcGridGc.MarcText = "";
                        this.marcGridGc.SetGridView();
                    }
                }
                wait.Close();
            }
        }

        private void excelMenuItem_Click(object sender, EventArgs e)
        {
            FrmCxExcel iso = new FrmCxExcel();
            iso.uid = this.uid;
            iso.kjm = this.kjm;
            iso.Show();

        }

        private void tgcMenuItem_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            using (ProgressHolder holder = new ProgressHolder(DoWorkTc, "剔除处理中..."))
            {
                holder.DoIt(this.dt.Rows.Count);
            }
            this.Search_BtnClick(sender, e);
            this.Cursor = Cursors.Default;
        }
        private void DoWorkTc(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            int max = (int)e.Argument;
            using (DbOperHandler _open = new Common().Doh())
            {
                string jms = string.Empty;
                /**
                for (int k = 0; k < this.dataGridXs.Rows.Count; k++)
                {
                    if (((DataGridViewCheckBoxCell)this.dataGridXs.Rows[k].Cells[3]).Value != DBNull.Value)
                    {
                        bool f = Convert.ToBoolean(((DataGridViewCheckBoxCell)this.dataGridXs.Rows[k].Cells[3]).Value);
                        if (f)
                        {
                            this.dataGridXs.Rows.RemoveAt(k);
                            k--;
                        }
                    }
                }
                **/
                for (int i = 0; i < max; i++)
                {
                    worker.ReportProgress((int)((double)i / max * 100), "正在处理第 " + i + " 条数据，请等待...");
                    bool f = false;
                    if (!string.IsNullOrEmpty(this.dt.Rows[i]["馆藏重复"].ToString().Trim())){
                        f = Convert.ToBoolean(this.dt.Rows[i]["馆藏重复"].ToString().Trim());
                    }
                    if (f)
                    {
                        jms += "'" + this.dt.Rows[i]["键码"].ToString().Trim() + "',";
                    }
                    //Thread.Sleep(1);
                }
                /**
                for (int k = 0; k < this.dataGridXs.Rows.Count; k++)
                {
                    if(((DataGridViewCheckBoxCell)this.dataGridXs.Rows[k].Cells[3]).Value != DBNull.Value)
                    {
                        bool f = Convert.ToBoolean(((DataGridViewCheckBoxCell)this.dataGridXs.Rows[k].Cells[3]).Value);
                        if (f)
                            jms += "'" + this.dataGridXs.Rows[k].Cells["jm"].Value.ToString() + "',";
                    }                  
                }
                **/
                if (!string.IsNullOrEmpty(jms))
                {
                    jms = jms.Substring(0, jms.Length - 1);
                    _open.Reset();
                    _open.ConditionExpress = "键码 in(" + jms + ")";
                    _open.Delete("待采书库");
                } 
            }
        }

        private void dpnMenuItem_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            using (ProgressHolder holder = new ProgressHolder(DoWorkPc, "剔除处理中..."))
            {
                holder.DoIt(this.dt.Rows.Count);
            }
            this.Search_BtnClick(sender, e);
            this.Cursor = Cursors.Default;
        }
        private void DoWorkPc(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            int max = (int)e.Argument;
            using (DbOperHandler _open = new Common().Doh())
            {
                string jms = string.Empty;
                for (int i = 0; i < max; i++)
                {
                    worker.ReportProgress((int)((double)i / max * 100), "正在处理第 " + i + " 条数据，请等待...");
                    bool f = Convert.ToBoolean(this.dt.Rows[i]["书目重复"].ToString().Trim());
                    if (f)
                    {
                        jms += "'" + this.dt.Rows[i]["键码"].ToString().Trim() + "',";
                    }
                    Thread.Sleep(5);
                }
                if (!string.IsNullOrEmpty(jms))
                {
                    jms = jms.Substring(0, jms.Length - 1);
                    _open.Reset();
                    _open.ConditionExpress = "键码 in(" + jms + ")";
                    _open.Delete("待采书库");
                }
            }
        }

        
        private void dataGridXS_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridXs.DataSource != null)
            {
                using (DbOperHandler _open = new Common().Doh())
                {
                    int cellIndex = e.ColumnIndex;
                    DataRowView current = this.BookData.Current as DataRowView;
                    string mainKay = current["键码"].ToString().Trim();
                    string ntype =this.dataGridXs.Rows[e.RowIndex].Cells[cellIndex].Value.GetType().Name;
                    //MessageBox.Show(ntype);
                    if (ntype.Equals("Int32"))
                    {
                        DataGridViewTextBoxCell gcCheck = (DataGridViewTextBoxCell)(this.dataGridXs.Rows[e.RowIndex].Cells[cellIndex]);
                        if (cellIndex == 4)
                        {
                            try
                            {
                                int yds = Convert.ToInt32(gcCheck.Value);
                                _open.Reset();
                                _open.ConditionExpress = "键码=" + mainKay;
                                _open.AddFieldItem("订数", yds);
                                _open.Update("待采书库");
                            }catch(Exception ex)
                            {
                                ShowMessageBox.ShowMessageBoxTimeout("输入的内容不合法:" + ex.Message, "出错啦！", MessageBoxButtons.OK, 3000);
                                _open.Reset();
                                _open.ConditionExpress = "键码=" + mainKay;
                                int yds = Convert.ToInt32(_open.GetField("待采书库","订数").ToString());
                                ((DataGridViewTextBoxCell)this.dataGridXs.Rows[e.RowIndex].Cells[cellIndex]).Value = yds;
                             }                            
                        }
                    }
                    if (ntype.Equals("Boolean"))
                    {
                        DataGridViewCheckBoxCell gcCheck = (DataGridViewCheckBoxCell)(this.dataGridXs.Rows[e.RowIndex].Cells[cellIndex]);
                        if (Convert.ToBoolean(gcCheck.Value))
                        {
                            if (cellIndex == 1)
                            {
                                string gys = this.dataGridXs.Rows[e.RowIndex].Cells["shtype"].Value.ToString();
                                if (gys.Equals("已通过"))
                                {
                                    _open.Reset();
                                    _open.ConditionExpress = "键码=" + mainKay;
                                    _open.AddFieldItem("是否采购", 1);
                                    _open.Update("待采书库");
                                }
                                else
                                {
                                    ShowMessageBox.ShowMessageBoxTimeout("该记录未审核或审核未通过！", "警告！", MessageBoxButtons.OK, 3000);
                                    gcCheck.Value = false;
                                }
                                   
                            }
                            if (cellIndex == 2)
                            {
                                _open.Reset();
                                _open.ConditionExpress = "键码=" + mainKay;
                                _open.AddFieldItem("书目重复", 1);
                                _open.Update("待采书库");
                            }
                            if (cellIndex == 3)
                            {
                                _open.Reset();
                                _open.ConditionExpress = "键码=" + mainKay;
                                _open.AddFieldItem("馆藏重复", 1);
                                _open.Update("待采书库");
                            }
                        }
                        else
                        {
                            if (cellIndex == 1)
                            {
                                _open.Reset();
                                _open.ConditionExpress = "键码=" + mainKay;
                                _open.AddFieldItem("是否采购", 0);
                                _open.Update("待采书库");
                            }
                            if (cellIndex == 2)
                            {
                                _open.Reset();
                                _open.ConditionExpress = "键码=" + mainKay;
                                _open.AddFieldItem("书目重复", 0);
                                _open.Update("待采书库");
                            }
                            if (cellIndex == 3)
                            {
                                _open.Reset();
                                _open.ConditionExpress = "键码=" + mainKay;
                                _open.AddFieldItem("馆藏重复", 0);
                                _open.Update("待采书库");
                            }
                        }
                    }
                    
                    //MessageBox.Show(mainKay + ":" + this.dataGridXs.CurrentCell.RowIndex + ":" + dataGridXs.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
                }
            }

        }

        private void datGridXs_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            dataGridXs.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }
        private void dataGridXs__CellClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }
        /// <summary>
        /// 全选预采购
        /// </summary>
        private void qcgMenuItem_Click(object sender, EventArgs e)
        {
            this.Search_BtnClick(sender, e);
            string jms = string.Empty;
            FrmWait wait = new FrmWait();
            wait.Show();
            for (int j = 0; j < this.dataGridXs.Rows.Count; j++)
            {
                wait.label4.Text = (j+ 1).ToString();
                Application.DoEvents();
                int shtype = Convert.ToInt32(this.dt.Rows[j]["审核状态"].ToString());
                string value = ((DataGridViewCheckBoxCell)this.dataGridXs.Rows[j].Cells[1]).Value.ToString();
                if (shtype==1)
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        bool f = Convert.ToBoolean(value);
                        if (!f)
                        {
                            jms += "'" + this.dt.Rows[j]["键码"].ToString() + "',";
                        }
                    }
                    else
                    {
                        jms += "'" + this.dt.Rows[j]["键码"].ToString() + "',";
                    }
                }
            }
            if (!string.IsNullOrEmpty(jms))
            {
                jms = jms.Substring(0, jms.Length - 1);
                using (DbOperHandler _open = new Common().Doh())
                {
                    _open.Reset();
                    _open.ConditionExpress = "键码 in(" + jms + ")";
                    _open.AddFieldItem("是否采购", 1);
                    int m =_open.Update("待采书库");
                    if(m>0)
                        this.Search_BtnClick(sender, e);
                }
            }
            wait.Close();
        }
        /// <summary>
        /// 取消全选预采购
        /// </summary>
        private void qxcgMenuItem_Click(object sender, EventArgs e)
        {
            this.Search_BtnClick(sender, e);
            FrmWait wait = new FrmWait();
            wait.Show();
            string jms = string.Empty;
            for (int j = 0; j < this.dataGridXs.Rows.Count; j++)
            {
                wait.label4.Text = (j + 1).ToString();
                Application.DoEvents();
                string value = ((DataGridViewCheckBoxCell)this.dataGridXs.Rows[j].Cells[1]).Value.ToString();
                if (!string.IsNullOrEmpty(value))
                {
                    bool f = Convert.ToBoolean(value);
                    if (f)
                        jms += "'" + this.dt.Rows[j]["键码"].ToString() + "',";
                }
                //((DataGridViewCheckBoxCell)this.dataGridXs.Rows[j].Cells[0]).Value = false;
            }
            if (!string.IsNullOrEmpty(jms))
            {
                jms = jms.Substring(0, jms.Length - 1);
                using (DbOperHandler _open = new Common().Doh())
                {
                    _open.Reset();
                    _open.ConditionExpress = "键码 in(" + jms + ")";
                    _open.AddFieldItem("是否采购", 0);
                    int m = _open.Update("待采书库");
                    if (m > 0)
                        this.Search_BtnClick(sender, e);
                }
            }
            wait.Close();
        }

        private void dataGridXs_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            int cellIndex = e.ColumnIndex;
            DataRowView current = this.BookData.Current as DataRowView;
            string mainKay = current["键码"].ToString().Trim();
            string ntype = this.dataGridXs.Rows[e.RowIndex].Cells[cellIndex].Value.GetType().Name;
            //MessageBox.Show(ntype);
            if (ntype.Equals("Int32"))
            {
                ShowMessageBox.ShowMessageBoxTimeout("输入的内容不合法", "出错啦！", MessageBoxButtons.OK, 3000);
                DataGridViewTextBoxCell gcCheck = (DataGridViewTextBoxCell)(this.dataGridXs.Rows[e.RowIndex].Cells[cellIndex]);
                gcCheck.Value = gcCheck.Value;
                e.Cancel = false;
            }
        }

        private void dataGridXs__RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            for (int k = 0; k < this.dataGridXs.Rows.Count; k++)
            {
                string gys = this.dataGridXs.Rows[k].Cells["shtype"].Value.ToString();
                if (gys.Equals("未通过"))
                {
                    this.dataGridXs.Rows[k].DefaultCellStyle.BackColor = System.Drawing.Color.Yellow;
                    this.dataGridXs.Rows[k].Cells[1].ReadOnly = true;
                }
            }
        }

        private void tshMenuItem_Click(object sender, EventArgs e)
        {
            FrmWait wait = new FrmWait();
            wait.Show();
            string jms = string.Empty;
            for (int k = 0; k < this.dataGridXs.Rows.Count; k++)
            {
                wait.label4.Text = (k + 1).ToString();
                Application.DoEvents();
                bool f = Convert.ToBoolean(((DataGridViewCheckBoxCell)this.dataGridXs.Rows[k].Cells[0]).Value);
                if (f)
                    jms +="'"+this.dataGridXs.Rows[k].Cells["jm"].Value.ToString()+"',";
            }
            if (!string.IsNullOrEmpty(jms))
            {
                jms = jms.Substring(0, jms.Length - 1);
                using (DbOperHandler _open = new Common().Doh())
                {
                    _open.Reset();
                    _open.ConditionExpress = "键码 in(" + jms + ")";
                    _open.AddFieldItem("审核状态", 1);
                    int m = _open.Update("待采书库");
                    if (m > 0)
                        this.Search_BtnClick(sender, e);
                }
            }
            wait.Close();
        }

        private void wtgMenuItem_Click(object sender, EventArgs e)
        {
            FrmWait wait = new FrmWait();
            wait.Show();
            string jms = string.Empty;
            for (int k = 0; k < this.dataGridXs.Rows.Count; k++)
            {
                wait.label4.Text = (k + 1).ToString();
                Application.DoEvents();
                bool f = Convert.ToBoolean(((DataGridViewCheckBoxCell)this.dataGridXs.Rows[k].Cells[0]).Value);
                if (f)
                    jms += "'" + this.dataGridXs.Rows[k].Cells["jm"].Value.ToString() + "',";
            }
            if (!string.IsNullOrEmpty(jms))
            {
                jms = jms.Substring(0, jms.Length - 1);
                using (DbOperHandler _open = new Common().Doh())
                {
                    _open.Reset();
                    _open.ConditionExpress = "键码 in(" + jms + ")";
                    _open.AddFieldItem("审核状态", 2);
                    int m = _open.Update("待采书库");
                    if (m > 0)
                        this.Search_BtnClick(sender, e);
                }
            }
            wait.Close();
        }

        private void shqMenuItem_Click(object sender, EventArgs e)
        {
            for (int k = 0; k < this.dataGridXs.Rows.Count; k++)
            {
                ((DataGridViewCheckBoxCell)this.dataGridXs.Rows[k].Cells[0]).Value = true;
            }
        }

        private void shxMenuItem_Click(object sender, EventArgs e)
        {
            for (int k = 0; k < this.dataGridXs.Rows.Count; k++)
            {
                ((DataGridViewCheckBoxCell)this.dataGridXs.Rows[k].Cells[0]).Value = false;
            }
        }

        private void dzddMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认要删除勾选的选书单吗？", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                using (DbOperHandler _open = new Common().Doh())
                {
                    this.GetZdd();
                    if (!string.IsNullOrEmpty(this.zdd))
                    {
                        _open.Reset();
                        _open.ConditionExpress = "征订号 in(" + this.zdd + ")";
                        _open.Delete("待采书库");
                        _open.Reset();
                        _open.ConditionExpress = "征订单号 in(" + this.zdd + ")";
                        _open.Delete("选书征订单");
                        this.SetTree();
                    }
                }
            }
        }
        /// <summary>
        /// 清空选书库
        /// </summary>
        private void ddckMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认要清空选书库吗？", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                using (DbOperHandler _open = new Common().Doh())
                {
                    _open.Reset();
                    _open.SqlCmd = "TRUNCATE TABLE 待采书库";
                    _open.ExecuteSqlNonQuery();
                    _open.Reset();
                    _open.SqlCmd = "TRUNCATE TABLE 选书征订单";
                    _open.ExecuteSqlNonQuery();
                    this.SetTree();
                    this.dataGridXs.DataSource = null;
                }
            }
        }
        
        private void Drydk(string mainKay,int jfjm,double dj,int cs,string ddh)
        {
            using (DbOperHandler _open = new Common().Doh())
            {
                _open.Reset();
                _open.ConditionExpress = "键码=" + jfjm;
                string jfbz = _open.GetField("采购经费库", "币种").ToString().Trim();
                List<int> zjmList = new List<int>();
                _open.Reset();
                _open.SqlCmd = " select 子键码 from 采购库 order by 子键码";
                DataTable dcg = _open.GetDataTable();
                if (dcg.Rows.Count > 0)
                {
                    for (int i = 0; i < dcg.Rows.Count; i++)
                    {
                        //Console.WriteLine(dcg.Rows[i]["子键码"].ToString());
                        zjmList.Add(Convert.ToInt32(dcg.Rows[i]["子键码"].ToString()));
                    }
                }
                int czjm = StringUtil.GetNextNumber(zjmList);
                List<int> jmList = new List<int>();
                _open.Reset();
                _open.SqlCmd = " select 主键码 from 馆藏书目库 order by 主键码";
                DataTable dr = _open.GetDataTable();
                if (dr.Rows.Count > 0)
                {
                    for (int i = 0; i < dr.Rows.Count; i++)
                    {
                        jmList.Add(Convert.ToInt32(dr.Rows[i]["主键码"].ToString()));
                    }
                }
                int zjm = StringUtil.GetNextNumber(jmList);
                _open.Reset();
                _open.ConditionExpress = "键码=" + mainKay;
                string marc = _open.GetField("待采书库", "marc").ToString();
                string zzd = _open.GetField("待采书库", "征订号").ToString();
                _open.Reset();
                _open.ConditionExpress = "征订单号='" + zzd + "'";
                int fxsjm = Convert.ToInt32(_open.GetField("选书征订单", "发行商键码").ToString());
                _open.Reset();
                _open.AddFieldItem("库键码", kjm);
                _open.AddFieldItem("主键码", zjm);
                _open.AddFieldItem("marc", marc);
                _open.AddFieldItem("处理日期", DateTime.Now);
                _open.Insert("馆藏书目库");
                SaveMarc.SaveForZjm(zjm, kjm);
                object[,] value = new object[14, 2] {
                        {"馆键码",this.gjm},
                        {"库键码",this.kjm},
                        {"主键码",zjm},
                        {"子键码",czjm},
                        { "发行商键码",fxsjm},
                        { "经费来源键码",jfjm},
                        { "采购员",this.uid},
                        { "订购单价",dj },
                        { "订购册数",cs},
                        { "订购金额",dj*cs},
                        { "预订日期",DateTime.Now},
                        { "状态","C"},
                        { "订单号",ddh},
                        {"币种",jfbz }
                    };
                _open.Reset();
                _open.AddFieldItems(value);
                _open.Insert("采购库");
                _open.Reset();
                _open.ConditionExpress = "键码=" + jfjm;
                double yz = Convert.ToDouble(_open.GetField("采购经费库", "预支").ToString()) + (0 - dj * cs);
                _open.AddFieldItem("预支", yz);
                _open.Update("采购经费库");
                _open.Reset();
                _open.ConditionExpress = "键码=" + mainKay;
                _open.Delete("待采书库");
            }
        }

        private void dydkMenuItem_Click(object sender, EventArgs e)
        {
            string shtype=this.dataGridXs.CurrentRow.Cells[4].Value.ToString();
            if (shtype.Equals("已通过"))
            {
                if (MessageBox.Show("确认要将当前记录导入预订库吗？", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                {
                    FrmCgydSetting f = new FrmCgydSetting();
                    f.uid = Convert.ToInt32(this.uid);
                    f.gjm = this.gjm;
                    if (f.ShowDialog() == DialogResult.OK)
                    {
                        string ddh = f.ddh;
                        int jfjm = f.jfjm;
                        //MessageBox.Show(this, jfjm + ":" + ddh);
                        DataRowView current = this.BookData.Current as DataRowView;
                        string mainKay = current["键码"].ToString().Trim();
                        int cs = Convert.ToInt32(current["订数"].ToString().Trim());
                        double dj = Convert.ToDouble(current["订价"].ToString().Trim());
                        this.Drydk(mainKay, jfjm, dj, cs, ddh);
                    }
                }
            }
            else
            {
                ShowMessageBox.ShowMessageBoxTimeout("该记录未审核或审核未通过！", "警告！", MessageBoxButtons.OK, 3000);
            }   
        }

        private void qydkMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认将要采购的记录导入预订库吗？", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                FrmCgydSetting f = new FrmCgydSetting();
                f.uid = Convert.ToInt32(this.uid);
                f.gjm = this.gjm;
                if (f.ShowDialog() == DialogResult.OK)
                {
                    string ddh = f.ddh;
                    int jfjm = f.jfjm;
                    FrmWait wait = new FrmWait();
                    wait.Show();
                    int m = 0;
                    for (int k = 0; k < this.dataGridXs.Rows.Count; k++)
                    {
                        string s = this.dataGridXs.Rows[k].Cells[1].Value.ToString();
                        if (!string.IsNullOrEmpty(s))
                        {
                            bool flag = Convert.ToBoolean(((DataGridViewCheckBoxCell)this.dataGridXs.Rows[k].Cells[1]).Value);
                            if (flag)
                            {
                                m++;
                                wait.label4.Text = (m).ToString();
                                Application.DoEvents();
                                string mainKay = this.dataGridXs.Rows[k].Cells["键码"].Value.ToString().Trim();
                                int cs = Convert.ToInt32(this.dataGridXs.Rows[k].Cells["订数"].Value.ToString().Trim());
                                double dj = Convert.ToDouble(this.dataGridXs.Rows[k].Cells["订价"].Value.ToString().Trim());
                                this.Drydk(mainKay, jfjm, dj, cs, ddh);
                            }
                        }
                    }
                    wait.Close();
                }
            }
        }

        private void allBtn_Click(object sender, EventArgs e)
        {
            for (int k = 0; k < this.dataGridXs.Rows.Count; k++)
            {
                ((DataGridViewCheckBoxCell)this.dataGridXs.Rows[k].Cells[0]).Value = true;
            }
        }

        private void qnaBtn_Click(object sender, EventArgs e)
        {
            for (int k = 0; k < this.dataGridXs.Rows.Count; k++)
            {
                ((DataGridViewCheckBoxCell)this.dataGridXs.Rows[k].Cells[0]).Value = false;
            }
        }

        private void tshBtn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认要将勾选的记录送审吗？", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                FrmWait wait = new FrmWait();
                wait.Show();
                string jms = string.Empty; int m = 0;
                for (int k = 0; k < this.dataGridXs.Rows.Count; k++)
                {
                    wait.label4.Text = (k + 1).ToString();
                    Application.DoEvents();
                    bool f = Convert.ToBoolean(((DataGridViewCheckBoxCell)this.dataGridXs.Rows[k].Cells[0]).Value);
                    if (f)
                        jms += "'" + this.dataGridXs.Rows[k].Cells["jm"].Value.ToString() + "',";
                }
                if (!string.IsNullOrEmpty(jms))
                {
                    jms = jms.Substring(0, jms.Length - 1);
                    using (DbOperHandler _open = new Common().Doh())
                    {
                        _open.Reset();
                        _open.ConditionExpress = "键码 in(" + jms + ")";
                        _open.AddFieldItem("是否送审", 1);
                        m = _open.Update("待采书库");
                    }
                }
                wait.Close();
                if (m > 0)
                    ShowMessageBox.ShowMessageBoxTimeout("送审成功【" + m + "】条", "提示", MessageBoxButtons.OK, 3000);
            }
        }

        private void xgdsMenuItem_Click(object sender, EventArgs e)
        {
            this.Search_BtnClick(sender, e);
            using (DbOperHandler _open = new Common().Doh())
            {
                FrmCxxg iso = new FrmCxxg();
                if (iso.ShowDialog() == DialogResult.OK)
                {
                    List<string[]> list = iso.list;
                    foreach (string[] str in list)
                    {
                        double dj = double.Parse(str[0]);
                        string jms = string.Empty;
                        foreach(DataRow dRow in this.dt.Rows)
                        {
                            double ydj = double.Parse(dRow["订价"].ToString().Trim());
                            if (ydj >= dj)
                                jms += "'" + dRow["键码"] + "',";
                        }
                        if (!string.IsNullOrEmpty(jms))
                        {
                            jms = jms.Substring(0, jms.Length - 1);
                            _open.Reset();
                            _open.ConditionExpress = "键码 in(" + jms + ")";
                            _open.AddFieldItem("订数", str[1]);
                            _open.Update("待采书库");
                        }
                    }
                }
            }
            this.Search_BtnClick(sender, e);
        }

        private void dceMenuItem_Click(object sender, EventArgs e)
        {
            FrmZrt f = new FrmZrt();
            f.wxlx = "待采书库";
            f.uid = this.uid;
            f.dt = dt;
            f.gjm = this.gjm;
            f.Show();
        }
    }
}
